from typing import Dict
from pydantic import BaseModel
from datetime import datetime
from utils.HashUtils import HashUtils
from utils.FileReader import FileReader
from Logger import Logger
import os

# 配置日志
logger = Logger.get_logger(__name__)


class Document(BaseModel):
    id: str  # 使用文件路径哈希作为唯一ID
    file_path: str  # 文本型的文件路径
    file_format: str  # 文件格式，例如 md/txt
    last_modified: datetime  # 最后修改时间
    file_size: int  # 整数型的文件大小
    text_content: Dict  # 
    
    @classmethod
    def create(cls, file_path: str, text_content: Dict):
        def get_file_format(file_path):
            _, ext = os.path.splitext(file_path)
            return ext[1:] if ext else 'txt'
        content= FileReader().read(file_path)
        doc_hash = HashUtils.get_content_hash(content)
        return cls(
            id=doc_hash,
            file_path=file_path,
            file_format=get_file_format(file_path),
            last_modified=datetime.now(),
            file_size=os.path.getsize(file_path),
            text_content=text_content,
        )

